package com.yitiao.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.yitiao.dto.ArticleQueryDTO;
import com.yitiao.dto.PageResult;
import com.yitiao.util.ImageWaterMark;
import com.yitiao.util.PdfUtil;
import com.yitiao.util.TextWaterMark;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yitiao.mapper.ArticleMapper;

import java.util.List;
import java.util.Map;

import com.yitiao.entity.Article;

@Service
@AllArgsConstructor
public class ArticleService {

    private final ArticleMapper articleMapper;


    public int batchInsert(List<Article> list) {
        return articleMapper.batchInsert(list);
    }

    public PageResult<Article> getArticleList(ArticleQueryDTO articleQueryDTO) {
        Page<Article> articlePage = articleMapper.selectPage(
                new Page<>(articleQueryDTO.getPageNum(), articleQueryDTO.getPageSize()),
                Wrappers.lambdaQuery(Article.class)
                        .like(StringUtils.isNotBlank(articleQueryDTO.getName()), Article::getName, articleQueryDTO.getName())
//                .eq(StringUtils.isNotBlank(articleQueryDTO.getName()),Article::getName,articleQueryDTO.getName())
//                .eq(StringUtils.isNotBlank(articleQueryDTO.getName()),Article::getName,articleQueryDTO.getName())
//                .eq(StringUtils.isNotBlank(articleQueryDTO.getName()),Article::getName,articleQueryDTO.getName())
        );
        return new PageResult<>(articlePage);
    }

    public List<Article> getAllArticle() {
        return articleMapper.selectList(Wrappers.lambdaQuery(Article.class));
    }


    public void generatePdf(List<Article> articles, OutputStream outputStream) {
        Document document = new Document();
        try {
            PdfWriter instance = PdfWriter.getInstance(document, outputStream);
            document.open();

            // 文字水印
            document.newPage();
            instance.setPageEvent(new TextWaterMark("一条coding"));

            // 图片水印
//            Resource resource = new ClassPathResource(pdfConfigProperties.getImage().getFile());
//            instance.setPageEvent(new ImageWaterMark(resource.getURL().getPath()));

            // 解决中文不显示问题
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题加粗
            Font fontChina18 = new Font(bfChinese, 18, Font.BOLD);
            Font fontChina15 = new Font(bfChinese, 15);
            Font fontChina10 = new Font(bfChinese, 10);

            // 获取pdf头文件信息
            Map<String, String> headInfo = new HashMap<>();
            headInfo.put("firstHeadInfo", "文章列表");
            // 空格
            Paragraph blank1 = new Paragraph(" ", new Font(bfChinese, 5));

            Paragraph firstTitle = new Paragraph(headInfo.get("firstHeadInfo"), fontChina18);
            firstTitle.setAlignment(Element.ALIGN_CENTER);// 居中
            document.add(firstTitle);

            // 添加空格
            document.add(blank1);

            // 3 创建表格
            PdfPTable table = new PdfPTable(7);// 表格总共几列
            table.setWidthPercentage(100);// 表格宽度为100%

            PdfUtil.addTableCell(table, "名称", fontChina15, false, false, 0, 0);
            PdfUtil.addTableCell(table, "目录", fontChina15, false, false, 0, 0);
            PdfUtil.addTableCell(table, "观看", fontChina15, false, false, 0, 0);
            PdfUtil.addTableCell(table, "喜欢", fontChina15, false, false, 0, 0);
            PdfUtil.addTableCell(table, "标签", fontChina15, false, false, 0, 0);
            PdfUtil.addTableCell(table, "摘要", fontChina15, true, false, 2, 0);


            articles.forEach(article -> {
                PdfUtil.addTableCell(table, article.getName(), fontChina10, true, false, 0, 0);
                PdfUtil.addTableCell(table, article.getCategoryId().toString(), fontChina10, true, false, 0, 0);
                PdfUtil.addTableCell(table, article.getViewCount().toString(), fontChina10, true, false, 0, 0);
                PdfUtil.addTableCell(table, article.getLikeCount().toString(), fontChina10, true, false, 0, 0);
                PdfUtil.addTableCell(table, article.getLabel(), fontChina10, true, false, 0, 0);
                PdfUtil.addTableCell(table, article.getDescription(), fontChina10, true, false, 2, 0);
            });

            document.add(table);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}
